<?php

/*------------------------------------------------
*
*   Explay Engine Content Management System.
*   Copyright © 2008 Golovdinov Alexander.
*
*-------------------------------------------------
*
*   Official site: wwww.alex-home.spb.ru/explay.html
*   Contact e-mail: golovdinov@gmail.com
*
*   GNU General Public License original source:
*   http://www.gnu.org/licenses/gpl-3.0.html
*
------------------------------------------------*/

if (!is_registered()) exit;

include $_SERVER['DOCUMENT_ROOT'].'/engine/templates.php';

if (isset($_POST['content']) && isset($_POST['module'])) {
	$module = addslashes ($_POST['module']);
	//$module = str_replace ('/', '', $module);
	$content_id = intval ($_POST['content']);
	
	if (isset($_POST['parent'])) {
		
		$get_c = $db->num_rows ($db->query ("SELECT * FROM ".DB_PEREFIX."_comments WHERE com_id = '".intval($_POST['parent'])."'"));
		if ($get_c == 1 || empty($_POST['parent'])) {
			
			if (isset($_POST['com_body']) && isset($_POST['parent']))
				if (isset($_POST['com_body']) && $_POST['com_body'] != '' && $_POST['com_body'] != 'undefined' && mb_strlen ($_POST['com_body']) < 5000)
					add_comment ('', '', $_POST['com_body'], $module, $content_id, '');
				else
					site_error ('Недопустимый текст комментария!');
		}
		else site_error ("Чертовщина какая-то! Этот комментарий был только что тут!");
	}
	
	if (isset($_POST['delete']) && is_admin() && isset($_POST['id'])) {
		$comment_id = intval($_POST['id']);
		
		$db->query ("DELETE FROM ".DB_PEREFIX."_comments WHERE com_id = $comment_id");
		
		$get = $db->query ("SELECT com_id, com_parent FROM ".DB_PEREFIX."_comments WHERE com_category = '$module' AND com_idi = '$content_id'");
		
		$array = array ();
		if ($db->num_rows ($get) > 0) {
			while (list($the_id, $parent) = $db->fetch_row ($get)) {
				$array[$parent][] = $the_id;
			}
			delete_com ($array[$comment_id], $array);
		}
		
	}
	
	include $_SERVER['DOCUMENT_ROOT'].'/modules/comments/comments.class.php';
	$oComments = new commnetsList;
	$oComments->build_comments ($module, $content_id, '/'.$module.'/'.$content_id.'.html');
	print $oComments->show_data ();
	unset ($oComments);
}
function delete_com ($arr, $array) {
	global $db;
	
	if (count($arr) > 0) {
		foreach ($arr as $comment) {
			$db->query ("DELETE FROM ".DB_PEREFIX."_comments WHERE com_id = '$comment'");
			if (isset($array[$comment])) delete_com ($array[$comment], $array);
		}
	}
	
}

